Game = Class.extend({

        init: function( context, 
                        gameField, 
                        backgroundImage, 
                        gunImage, 
                        images, 
                        audios, 
                        gunAudio, 
                        munitions,
                        playerMoney,
                        playerPoint,
                        playerCounter
                        ) {
		this.context = context;

                this.gameField = gameField;

                this.backgroundImage = backgroundImage;
                this.image = images;
                this.audios = audios;
                
                this.gunImage = gunImage;
                this.gunAudio = gunAudio;
                this.munitions = munitions;

		// creating the objects
		this.initObjects();

		this.playerMoney = playerMoney;
		this.playerPoint = playerPoint;
                this.playerCounter = playerCounter;
                
                this.start();
                
	},
        
	initObjects: function() {
     
                this.gunAnimation = new Gun(
                        this.context,
                        200,
                        200,
                        this.gunImage,
                        this.munitions
                );
        
	},
	
	initEnemy: function() {
		var self = this;
    	
	      //Level 1
	        if(this.backgroundImage == this.image[10]){
		        self.enemyLevel1 = new Level1(self.context, self.image);
		        self.enemyLevel1.initEnemy();
	        };
	        
	      //Level 2
	        if(this.backgroundImage == this.image[11]){
	        	self.enemyLevel2 = new Level2(self.context, self.image);
	        	self.enemyLevel2.initEnemy();

	        };
	      
	      //Level 3
	        if(this.backgroundImage == this.image[12]){
	        	self.enemyLevel3 = new Level3(self.context, self.image);
		        self.enemyLevel3.initEnemy();
	        };
	        
	},      
        
        
        start: function() {
            
        this.gunMunitions = this.gunAnimation.munitions;
        
        this.gunAnimation.clear(0,0,1024,720);
        var self = this;
        
        var gameField = document.getElementById(this.gameField);
        gameField.style.cursor = "url('../images/game/cursor.png'), pointer";
        gameField.style.backgroundImage = "url("+this.backgroundImage.src+")";
        
        
        this.initEnemy();
        
        var self=this;
            var x;
            var flag;
            
            
            var gameField = document.getElementById(this.gameField);
            document.addEventListener('mousemove', function move(event){
                x = event.pageX-gameField.offsetLeft;
            });
            
            
            var gunInterval = setInterval(function(){
                self.gunAnimation.clear(0,503,1024,170);
                self.gunAnimation.draw(0,0,x);

            },50);
            
            document.onmousedown = function (e) {
                var mouseCordinateX = e.offsetX;
                var mouseCordinateY = e.offsetY;

                if(self.gunAnimation.munitions > 0 ){
                    self.gunAnimation.munitions--;
                    clearInterval(gunInterval);

                    var arrayWithImages = self.gunAnimation.shoot(0);
                    var count=0;

                    self.gunAudio.load();
                    self.gunAudio.play();
                    if(flag === undefined || flag === false){
                        var shootingInterval=setInterval(function(){
                            flag = true;
                            count++;
                            if(count === 4){
                                flag = false;
                                count = 0;
                                clearInterval(shootingInterval);
                                self.gunAnimation.clear(0,503,1024,170);
                                self.gunAnimation.draw(0,0,x);

                                gunInterval = setInterval(function(){
                                    self.gunAnimation.clear(0,503,1024,170);
                                    self.gunAnimation.draw(0,0,x);

                                },50);
                            };
                            self.gunAnimation.clear(0,503,1024,170);
                            self.gunAnimation.draw(arrayWithImages[count],0,x);

                        },50);
                    }
                    
// ********************* Enemy ************************
              
                //Level 1
    	        if(self.backgroundImage == self.image[10]){
    	        	self.enemyLevel1.checkedPosition([mouseCordinateX, mouseCordinateY]);
    	        }
    	        	            
                //Level 2
                if(self.backgroundImage == self.image[11]){
                	self.enemyLevel2.checkedPosition([mouseCordinateX, mouseCordinateY]);  
                };
    	          //Level 3
                if(self.backgroundImage == self.image[12]){
                	self.enemyLevel3.checkedPosition([mouseCordinateX, mouseCordinateY]);
                };
                  
                
                if(self.playerCounter === 0){
                document.removeEventListener('mousemove', move())
                clearInterval(gunInterval);
                self.gunAnimation.clear(0,0,1024,720);
                
                var pickLevel = new LevelGunPick(
                    self.context,
                    self.image,
                    self.audios,
                    self.playerMoney,
                    self.playerPoint
                            );
                    pickLevel.drawImages();
                }

                    };

                }
      
        document.onkeydown = function (e){
            var count=0;
            var spriteCount = 200;

            if(flag === undefined || flag === false){
                
                
                if(e.keyCode === 32){
                self.audios[3].load();
                self.audios[3].play(); 
                clearInterval(gunInterval);
                    var reloadInterval=setInterval(function(){
                        flag = true;
                        var arrayWithImages = self.gunAnimation.reload(0);

                        self.gunAnimation.clear(0,503,1024,200);

                        self.gunAnimation.draw(arrayWithImages[count],spriteCount,x);

                        count++;
                        if(count === 4 && spriteCount === 200){
                            count = 0;
                            spriteCount = 400;
                            self.gunAnimation.clear(0,503,1024,170);

                            self.gunAnimation.draw(arrayWithImages[count],spriteCount,x);
                        }else if(count === 4 && spriteCount === 400){
                            flag = false;
                            clearInterval(reloadInterval);
                            self.gunAnimation.clear(0,503,1024,170);
                            self.gunAnimation.draw(0,0,x);
                            
                            self.gunAnimation.munitions = self.gunMunitions;
                            gunInterval = setInterval(function(){
                                self.gunAnimation.clear(0,503,1024,170);

                                self.gunAnimation.draw(0,0,x);
                            },50);
                        }
                    },150);
                };
            }
        };
	
	}, // start method


            
});